package com.cm.sys.framework.execption;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;

/**
 * 统一的异常处理类
 * 
 * @author yxq18
 *
 */
@Order(value = -1)
@ControllerAdvice
public class BusinessExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(BusinessExceptionHandler.class);

    @ExceptionHandler
    @ResponseBody
    public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
        JSONObject json = new JSONObject();
        logger.error(e.getMessage(), e);

        if (e instanceof org.mybatis.spring.MyBatisSystemException) {
            if ((e.getCause() instanceof org.apache.ibatis.exceptions.PersistenceException)
                && e.getCause().getCause() instanceof BusinessException) {
                e = (BusinessException)e.getCause().getCause();
            }
        }

        if (e instanceof BusinessException) {
            json.put("code", ((BusinessException)e).getCode());
            json.put("message", ((BusinessException)e).getMessage());
        } else if (e instanceof HttpRequestMethodNotSupportedException) {
            json.put("code", ApiResponseCode.NOT_FOUND.getCode());
            json.put("message", ApiResponseCode.NOT_FOUND.getMessage());
        } else if (e instanceof MissingServletRequestParameterException) {
            json.put("code", ApiResponseCode.OTHER.getCode());
            json.put("message", "请求缺少必传参数");
        } else if (e instanceof MaxUploadSizeExceededException) {
            json.put("code", ApiResponseCode.VAILDATE_ERROR.getCode());
            json.put("message", ApiResponseCode.VAILDATE_ERROR.getMessage() + " " + "上传文件不能大于:");
        } else if (e instanceof java.sql.SQLException) {
            json.put("code", ApiResponseCode.SQL_ERROR.getCode());
            json.put("message", ApiResponseCode.SQL_ERROR.getMessage());
        } else if (e instanceof NumberFormatException) {
            json.put("code", ApiResponseCode.TYPE_CONVERSION_EXCEPTION.getCode());
            json.put("message", ApiResponseCode.TYPE_CONVERSION_EXCEPTION.getMessage());
        }else if (e instanceof org.springframework.web.bind.MethodArgumentNotValidException) {
            json.put("code", ApiResponseCode.TYPE_CONVERSION_EXCEPTION.getCode());
            json.put("message", ApiResponseCode.TYPE_CONVERSION_EXCEPTION.getMessage());
        } else {
            json.put("code", ApiResponseCode.ERROR.getCode());
            json.put("message", ApiResponseCode.ERROR.getMessage());
        }

        e.printStackTrace();
        return json.toString();
    }
}
